/*
 * Copyright (c) 2008-20018 Saša Stamenković <umpirsky@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice,
 *    	this list of conditions and the following disclaimer in the documentation
 *    	and/or other materials provided with the distribution.
 *    * Neither the name of the <ORGANIZATION> nor the names of its contributors
 *    	may be used to endorse or promote products derived from this software without
 *    	specific prior written permission.
 *
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *    IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 *    OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package umpirsky;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * Custom files chooser.
 * Validates file size, number of files, image preview...
 *
 * @author Saša Stamenković <umpirsky@gmail.com>
 */
public class UmpirskyFileChooser extends JFileChooser {
	/**
	 * Serial
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Maximum number of files that can be selected
	 */
	private short numberOfFilesMax;

	/**
	 * Maximum MBs allowed for upload
	 */
	private short uploadMbMax;

	/**
	 * Maximum MBs allowed for one file
	 */
	private short fileMbMax;

	/**
	 * Set numberOfFilesMax
	 *
	 * @param numberOfFilesMax maximum number of files that can be selected
	 */
	public void setNumberOfFilesMax(short numberOfFilesMax) {
		this.numberOfFilesMax = numberOfFilesMax;
	}

	/**
	 * Set uploadMbMax
	 *
	 * @param uploadMbMax maximum upload size
	 */
	public void setUploadMbMax(short uploadMbMax) {
		this.uploadMbMax = uploadMbMax;
	}

	/**
	 * Set fileMbMax
	 *
	 * @param fileMbMax maximum file size
	 */
	public void setFileMbMax(short fileMbMax) {
		this.fileMbMax = fileMbMax;
	}

	/**
	 * Override JFileChooser method, called when user clicks open.
	 * Used to validate number of files and file size. Pops some warnings if limits are bounded.
	 */
	@Override
	public void approveSelection() {
		// Get selected files
		ArrayList<File> files = new ArrayList<File>(Arrays.asList(this.getSelectedFiles()));

		// Get number of files and size, and check for maximum file size
		short numberOfFiles = 0;
		long uploadB = 0;
		for (File file : files) {
			if (0 != this.fileMbMax && this.fileMbMax < ((float) file.length()/(1024*1024))) {
				this.showWarning("Maximum file size is " + this.fileMbMax + "MB", "File : " + file.getName() + " size is " + (float) file.length()/(1024*1024) + "MB." + "\nPlease check your selection once again.");
				return;
			}
			numberOfFiles++;
			uploadB += file.length();
		}

		// Check number of files
		numberOfFiles += UmpirskyUploader.getFilesCount();
		if(!checkFilesCount(numberOfFiles)) {
			this.showWarning("Maximum " + this.numberOfFilesMax + " files allowed", "You have selected " + numberOfFiles + "." + "\nPlease check your selection once again.");
			return;
		}

		// Check maximum upload size
		short uploadMB = (short) (uploadB/(1024*1024));
		uploadMB += UmpirskyUploader.getFilesSize();
		if(!checkFilesSize(uploadMB)) {
			this.showWarning("Maximum upload size is " + this.uploadMbMax + "MB", "You have selected " + uploadMB + "MB." + "\nPlease check your selection once again.");
			return;
		}

		super.approveSelection();
	}

	/**
	 * Checks if user selected more files than is allowed.
	 *
	 * @param numberOfFiles number of files selected
	 * @return boolean
	 */
	private boolean checkFilesCount(short numberOfFiles) {
		if (0 == this.numberOfFilesMax) {
			return true;
		}

		if (this.numberOfFilesMax >= numberOfFiles) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Checks if user selected more files than is allowed in MB.
	 *
	 * @param uploadMB upload size in MB
	 * @return boolean
	 */
	private boolean checkFilesSize(short uploadMB) {
		if (0 == this.uploadMbMax) {
			return true;
		}

		if (this.uploadMbMax >= uploadMB) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Shows warning with title and message
	 *
	 * @param title title
	 * @param message warning message
	 */
	private void showWarning(String title, String message) {
		JOptionPane.showMessageDialog(this, message, title, JOptionPane.WARNING_MESSAGE);
	}
}